package com.jt.rediscontroller;
/*
  基于redis模拟前端请求访问
  Post新增
  Get查询
  均采用restful格式
 */
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/redis/demo01")
public class RedisController01 {
    @Autowired
    private RedisTemplate redisTemplate;
   @PostMapping("/{id}") //redis作存储操作：POST http://localhost:8080/redis/demo01/100
    public String insert(@PathVariable("id") Integer id){ //接收前端的id
       HashOperations hash = redisTemplate.opsForHash();
       hash.put("blog", "id", id);//参数值传进来
        return "插入成功！";
   }
    @GetMapping("/{field}")  //取出操作： GET http://localhost:8080/redis/demo01/filed
    public String doSelect(@PathVariable("field") String field){
        HashOperations ho = redisTemplate.opsForHash();
        return "value is "+ String.valueOf(ho.get("blog","id"));//value is 100
    }
}


